package
{
	import view.components.Thumbnail;

	import com.greensock.TimelineMax;
	import com.greensock.TweenMax;
	import com.greensock.plugins.BlurFilterPlugin;
	import com.greensock.plugins.HexColorsPlugin;
	import com.greensock.plugins.TintPlugin;
	import com.greensock.plugins.TweenPlugin;

	import flash.display.Sprite;
	import flash.filters.BlurFilter;

	public class FX
	{
		
		public static function initApp( view:Object, f:Function ):void
		{
			view.alpha = 0;
			TweenMax.to( view, ApplicationFacade.SPEED, {alpha:1, onComplete:f} );
		}
		
		public static function transitionViews( objs:Object, props:Object, f:Function = null ):void
		{
			TweenPlugin.activate( [HexColorsPlugin, BlurFilterPlugin, TintPlugin] );
			
			var tl:TimelineMax = new TimelineMax( { paused:true,
													onComplete:props.f});
									
			tl.append( TweenMax.to( objs.bckgrd, 
									ApplicationFacade.SPEED,
									{ tint:props.bc } ),
									-ApplicationFacade.SPEED );
									
			tl.append( TweenMax.to( objs.bckgrdGrd, 
									ApplicationFacade.SPEED, 
									{ alpha:props.ga } ),
									-( ApplicationFacade.SPEED )  );
															
			tl.append( TweenMax.to( props.gcI, 
									ApplicationFacade.SPEED, 
									{ hexColors:{left:props.gcO.left, right:props.gcO.right}, 
									onUpdate:props.u } ),
									-( ApplicationFacade.SPEED ) );
			
			tl.play();
		}
		
		public static function fxMixImagesTransition( s:Sprite, f:Function = null ):void
		{
			var imA:Object = s.getChildAt( 0 );
			var imB:Object = s.getChildAt( 1 );
			
			imA.alpha = 1;
			imB.alpha = 0;
			
			var timeline:TimelineMax = new TimelineMax( { paused:true } );
			
			timeline.appendMultiple( [ TweenMax.to( imA, ApplicationFacade.SPEED, {alpha:0} ),
									   TweenMax.to( imB, ApplicationFacade.SPEED, {alpha:1, onComplete:f} ) ] );
			
			timeline.play();
		}
		
		public static function galleryLeftFadeTransition( c:Sprite, f:Function = null ):void
		{			
			var thumbs:Array = new Array( c.numChildren );
			
			var i:uint;
			for( i = 0; i < thumbs.length; i++ )
			{
				thumbs[i] = c.getChildAt( i ) as Thumbnail;
				thumbs[i].alpha = 0;
			}
			
			thumbs.sortOn("x", Array.NUMERIC);
			
			var timeline:TimelineMax = new TimelineMax( { paused:true } );
			
			timeline.appendMultiple( TweenMax.allTo( thumbs, ApplicationFacade.SPEED, {alpha:1}, 0.03 ) );
			
			timeline.play();
		}
		
		public static function galleryShuffleTransition( c:Sprite, f:Function = null ):void
		{
			var thumbs:Array = new Array( c.numChildren );
			
			var i:uint;
			for( i = 0; i < thumbs.length; i++ )
			{
				thumbs[i] = c.getChildAt( i ) as Thumbnail;
				thumbs[i].alpha = 0;
			}
			
			var arr2:Array = [];
			
			while (thumbs.length > 0)
			{
				arr2.push( thumbs.splice( Math.round(Math.random() * ( thumbs.length - 1 )), 1 )[0] );
			}
		    
		    var timeline:TimelineMax = new TimelineMax( { paused:true } );
			
			timeline.appendMultiple( TweenMax.allTo( arr2, ApplicationFacade.SPEED / 2, {alpha:1}, 0.02 ) );
			
			timeline.play();
		}
		
		public static function fadeIn( a:Object, f:Function = null ):void
		{
			a.alpha = 0;
			TweenMax.to( a, ApplicationFacade.SPEED, { alpha:1, onComplete:f } );
		}
		
		public static function fadeInFrom( a:Object, f:Function = null ):void
		{
			TweenMax.to( a, ApplicationFacade.SPEED, { autoAlpha:1, onComplete:f } );
		}
		
		public static function fadeInTo( a:Object, val:Number, f:Function = null ):void
		{
			a.alpha = 0;
			TweenMax.to( a, ApplicationFacade.SPEED, { alpha:val, onComplete:f } );
		}
		
		public static function fadeOutTo( a:Object, val:Number, f:Function = null ):void
		{
			a.alpha = 1;
			TweenMax.to( a, ApplicationFacade.SPEED, { alpha:val, onComplete:f } );
		}
		
		public static function fadeOutFrom( a:Object, f:Function = null ):void
		{
			TweenMax.to( a, ApplicationFacade.SPEED, { autoAlpha:0, onComplete:f } );
		}
		
		public static function fadeInMultiple( a:Array, f:Function = null ):void
		{
			var i:uint;
			for( i = 0; i < a.length; i++ )
			{
				a[i].alpha = 0;
			}
			
			var timeline:TimelineMax = new TimelineMax( { paused:true, onComplete:f } );
			
			timeline.appendMultiple( TweenMax.allTo( a, ApplicationFacade.SPEED / 2, {alpha:1}, 0.02 ) );
			
			timeline.play();
		}
		
		public static function fadeOutMultiple( a:Array, f:Function = null ):void
		{
			var i:uint;
			for( i = 0; i < a.length; i++ )
			{
				a[i].alpha = 1;
			}
			
			var timeline:TimelineMax = new TimelineMax( { paused:true, onComplete:f } );
			
			timeline.appendMultiple( TweenMax.allTo( a, ApplicationFacade.SPEED / 2, {alpha:0}, 0.02 ) );
			
			timeline.play();
		}
		
		public static function fadeOut( a:Object, f:Function = null ):void
		{
			a.alpha = 1;
			TweenMax.to( a, ApplicationFacade.SPEED, { alpha:0, onComplete:f } );
		}
		
		public static function fadeInGridLines( a:Object, aAlpha:Number, f:Function = null ):void
		{
			a.alpha = 0;
			TweenMax.to( a, ApplicationFacade.SPEED, { alpha:aAlpha, onComplete:f } );
		}
		
		public static function fadeOutGridLines( a:Object, aAlpha:Number, f:Function = null ):void
		{
			a.alpha = aAlpha;
			TweenMax.to( a, ApplicationFacade.SPEED, { alpha:0, onComplete:f } );
		}
		
		public static function menuBtnOverFX( a:Object, c:Number, b:Number, f:Function = null ):void
		{
			if( b > 0 ) a.filters = [ new BlurFilter( b, b, 3 ) ];
			
			TweenMax.to( a, ApplicationFacade.SPEED, {tint:c});
			
			if( b > 0 ) 
				TweenMax.to( a, ApplicationFacade.SPEED, { blurFilter:{blurX:0, blurY:0} } );
		}
		
		public static function tint( a:Object, value:Number, f:Function = null ):void
		{
			TweenMax.to( a, ApplicationFacade.SPEED, { tint:value, onComplete:f } );
		}
		
		public static function translate( a:Object, xPos:Number, yPos:Number, f:Function = null ):void
		{
			TweenMax.to( a, ApplicationFacade.SPEED / 2, { x:xPos } );
			TweenMax.to( a, ApplicationFacade.SPEED / 2, { y:yPos, onComplete:f } );
		}
		
		public static function translateY( a:Object, yPos:Number, f:Function = null ):void
		{
			TweenMax.to( a, ApplicationFacade.SPEED / 2, { y:yPos, onComplete:f } );
		}
		
		public static function scaleX( a:Object, xVal:Number, f:Function = null ):void
		{
			TweenMax.to( a, ApplicationFacade.SPEED / 3, { scaleX:xVal, onComplete:f } );
		} 
	}
}
